package com.weex.app.util;


import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LocalLogUtils {
    /**
     * 保存错误信息到文件中
     *
     * @param name name
     * @return 返回文件名称, 便于将文件传送到服务器
     */
    public static String saveInfo2File(String name, String errorcode) {
        // 用于格式化日期,作为日志文件名的一部分
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA);
        try {
            long timestamp = System.currentTimeMillis();
            String time = formatter.format(new Date());
            String fileName = name + "-" + time + "-" + timestamp + ".txt";
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                String path = File.separator + "sdcard" + File.separator + "crash/";
                File dir = new File(path);
                if (!dir.exists()) {
                    boolean isSuccess = dir.mkdirs();
                    System.out.println(isSuccess);
                }
                FileOutputStream fos = new FileOutputStream(path + fileName);
                fos.write(errorcode.getBytes());
                fos.close();
            }
            return fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
